package com.action.park;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.log4j.Logger;

import com.base.BaseAction;
import com.bean.Userinfo;
import com.dao.ParkOrderDao;
import com.service.UserinfoService;
import com.util.EncoderHandler;
import com.util.GUtil;

public class DriverAction extends BaseAction {
	private static final Logger log = Logger.getLogger(ParkOrderDao.class);
	Boolean queryMyOrder=false;//true: 收费员查询自己的订单 ; false查询所有订单
	UserinfoService  service;
	List<Object> list=null;
	Userinfo userinfo;
	Integer flag;
	Timestamp ts = new Timestamp(System.currentTimeMillis());
	SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
	String date = sdf.format(ts);
	@Override
	public String handle() throws Exception {
		if(id!=null && id>0)
			return this.view();
		return this.query();
	}
	@Override
	public String query() {
		this.setHeaderTitle("所有司机会员列表");
		try {
			list=service.finduserlist(userinfo,page);
			session.put("satus", service.findststus());
			return GUtil.SUCCESS;
		} catch (Exception e) {
			e.getStackTrace();
			return GUtil.LOGIN;
		}
	}
	/*
	 * 锁定
	 */
	public String locking() throws Exception {
		session.put("userinfo", service.gett(Long.valueOf(id)));
		Userinfo or = (Userinfo) session.get("userinfo");
		if(id==null || id<1)
			return GUtil.ERROR;
		else{
			if(service.lock(Long.valueOf(id),0).equals(GUtil.SUCCESS)){
				this.setMsg("锁定成功");
				this.setLogmsg("[" + date + "][后台管理员:" + user.getUserCode() + ","
						+ user.getUserName() + "][把司机会员:"
						+ or.getRealName() + "已锁定");
			}else{
				this.setMsg("锁定失败");
				this.setLogmsg("[" + date + "][后台管理员:" + user.getUserCode() + ","
						+ user.getUserName() + "][把司机会员:"
						+ or.getRealName() + "锁定失败");
			}
		}
		return this.query() ;
	}
	/*
	 * 解锁
	 */
	public String delocking() throws Exception {
		session.put("userinfo", service.gett(Long.valueOf(id)));
		Userinfo or = (Userinfo) session.get("userinfo");
		if(id==null || id<1)
			return GUtil.ERROR;
		else{
			if(service.lock(Long.valueOf(id),1).equals(GUtil.SUCCESS)){
				this.setMsg("成功解锁");
				this.setLogmsg("[" + date + "][后台管理员:" + user.getUserCode() + ","
					+ user.getUserName() + "][把司机会员:"
					+ or.getRealName() + "已成功解锁");
			}else{
				this.setMsg("解锁失败");
				this.setLogmsg("[" + date + "][后台管理员:" + user.getUserCode() + ","
					+ user.getUserName() + "][把司机会员:"
					+ or.getRealName() + "已成功解锁");
			}
		}
		return this.query() ;
	}
	@Override
	public String delete() throws Exception {
		session.put("userinfo", service.gett(Long.valueOf(id)));
		Userinfo or = (Userinfo) session.get("userinfo");
		if(id==null || id<1)
			return GUtil.ERROR;
		else{
			if(service.delete(Long.valueOf(id),-1).equals(GUtil.SUCCESS)){
				this.setMsg("删除成功");
				this.setLogmsg("[" + date + "][后台管理员:" + user.getUserCode() + ","
						+ user.getUserName() + "][把司机会员:"
						+ or.getRealName() + "成功删除");
			}else{
				this.setMsg("删除失败");
				this.setLogmsg("[" + date + "][后台管理员:" + user.getUserCode() + ","
						+ user.getUserName() + "][删除司机会员:"
						+ or.getRealName() + "失败");
			}
		}
		return this.query() ;
	}

	@Override
	public String view() throws Exception {
		if(id==null || id<1)
			return GUtil.ERROR;
		userinfo = (Userinfo) service.gett(Long.valueOf(id));
		if(userinfo==null)
			return GUtil.ERROR;
		return GUtil.VIEW;
	}

	@Override
	public String add() {
		this.setHeaderTitle("添加司机会员信息");
		//beforeAdd
		if(userinfo==null){
			//beforeUpdate
			session.remove("userinfo");
			flag = 0;
			return GUtil.EDIT;
		}
		
		//add
		try {
			Timestamp ts = new Timestamp(System.currentTimeMillis());
			userinfo.setCredit(0);
			userinfo.setAccountBalance(0.0);
			userinfo.setScore(0);

			userinfo.setRealName(userinfo.getRealName());
			userinfo.setPhone(userinfo.getPhone());
			userinfo.setCarNo(userinfo.getCarNo());
			userinfo.setEmail(userinfo.getEmail());
			userinfo.setPayPasswd(userinfo.getPayPasswd());
			userinfo.setIdCard(userinfo.getIdCard());
			if(userinfo.getPayPasswd()!=null){
				if(!userinfo.getPayPasswd().trim().isEmpty()){
					userinfo.setPayPasswd(EncoderHandler.EncoderPasswordBase64Md5(userinfo.getPayPasswd()));
				}
			}
			
			//password check
			if(userinfo.getUserPasswd()!=null){
				if(!userinfo.getUserPasswd().trim().isEmpty()){
					userinfo.setUserPasswd(EncoderHandler.EncoderPasswordBase64Md5(userinfo.getUserPasswd()));
				}
			}
			
			userinfo.setUserPasswd(userinfo.getUserPasswd());
			userinfo.setGender(userinfo.getGender());
			userinfo.setUserPhoto(userinfo.getUserPhoto());
			userinfo.setIntroduction(userinfo.getIntroduction());

			Short s=1;
			userinfo.setStatus(s);
			userinfo.setCreateTime(ts);
			userinfo.setUpdateTime(ts);
			userinfo.setLoginTime(ts);
			Short type=1;
			userinfo.setClientType(type);
			service.add(userinfo);
			this.setMsg("添加成功！");
			userinfo=null;
			return this.query();
		} catch (Exception e) {
			this.setMsg("操作失败！");
			e.getStackTrace();
			return GUtil.ERROR;
		}
	}

	@Override
	public String update(){
		flag = 1;
		this.setHeaderTitle("修改司机会员信息");
		try{
			if(id==null){
				System.out.println("id不存在"+id);
				return GUtil.ERROR;
			}else if(userinfo==null){
				//beforeUpdate
				userinfo = (Userinfo)service.gett(Long.valueOf(id));
				session.put("userinfo", userinfo);
				if(userinfo==null){
					this.setLogmsg("信息不存在");
					return GUtil.ERROR;
				}
				return GUtil.EDIT;
			}
			//update
			Userinfo ui  = (Userinfo)session.get("userinfo");
			if(ui==null){
				this.setMsg("异常");
				return GUtil.ERROR;
			}
		
			
			Timestamp ts = new Timestamp(System.currentTimeMillis());
			ui.setUpdateTime(ts);	
			ui.setPhone(userinfo.getPhone());
			ui.setRealName(userinfo.getRealName());
			//password check
			if(userinfo.getUserPasswd()!=null){
				if(!userinfo.getUserPasswd().trim().isEmpty()){
					ui.setUserPasswd(EncoderHandler.EncoderPasswordBase64Md5(userinfo.getUserPasswd()));
				}
			}
			if(ui.getPayPasswd()!=null){
				if(!ui.getPayPasswd().trim().isEmpty()){
					ui.setPayPasswd(EncoderHandler.EncoderPasswordBase64Md5(userinfo.getPayPasswd()));
				}
			}
			ui.setGender(userinfo.getGender());
			ui.setEmail(userinfo.getEmail());
			ui.setCarNo(userinfo.getCarNo());
			ui.setIntroduction(userinfo.getIntroduction());
			//ui.setUserPhoto(userPhoto);头像
			ui.setIdCard(userinfo.getIdCard());
			service.update(ui);

			this.setMsg("修改成功");

			userinfo = null;
			return this.query();

		}catch (Exception e) {
			this.setMsg("修改失败");
			e.printStackTrace();
			return GUtil.EDIT;
		}
	}

	public UserinfoService getService() {
		return service;
	}

	public void setService(UserinfoService service) {
		this.service = service;
	}

	public List<Object> getList() {
		return list;
	}
	
	public void setList(List<Object> list) {
		this.list = list;
	}

	public Userinfo getUserinfo() {
		return userinfo;
	}

	public void setUserinfo(Userinfo userinfo) {
		this.userinfo = userinfo;
	}
	public Integer getFlag() {
		return flag;
	}
	public void setFlag(Integer flag) {
		this.flag = flag;
	}
	@Override
	public boolean validate(Object obj) throws Exception {
		return false;
	}

}
